package com.dingyh.shopwebbackground.controller;


import com.dingyh.shopwebbackground.entity.Business;
import com.dingyh.shopwebbackground.entity.Orders;
import com.dingyh.shopwebbackground.entity.ReturnOrder;
import com.dingyh.shopwebbackground.service.BusinessService;
import com.dingyh.shopwebbackground.service.OrdersService;
import com.dingyh.shopwebbackground.service.ReturnOrderService;
import com.dingyh.shopwebbackground.util.CommonResult;
import com.dingyh.shopwebbackground.util.UUIDUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author dingyuhan
 * @since 2023-03-23
 */
@RestController
@CrossOrigin
@RequestMapping("/return-order")
public class ReturnOrderController {
    @Autowired
    ReturnOrderService returnOrderService;

    @Autowired
    OrdersService ordersService;

    @Autowired
    BusinessService businessService;

    /**
     * 退货申请
     * @param returnOrder
     * @return
     */
    @RequestMapping("/returnAdd")
    public CommonResult<Object> returnAdd(ReturnOrder returnOrder){
        ReturnOrder returnOrderCurr =  ReturnOrder.builder()
                .id(UUIDUtil.generator())
                .orderId(returnOrder.getOrderId())
                .logisticsCode(returnOrder.getLogisticsCode())
                .logisticsCompany(returnOrder.getLogisticsCompany())
                .saleAddress(returnOrder.getSaleAddress())
                .build();
        boolean result = returnOrderService.save(returnOrderCurr);
        if (result) {
            return new CommonResult(200, "退货成功！", null);
        } else {
            return new CommonResult(500, "退货失败！", null);
        }
    }

    /**
     * 查找商家的退货地址
     * @param orderId
     * @return
     */
    @RequestMapping("/selectSaleAddress")
    public CommonResult<Object> selectSaleAddress(String orderId){
        Orders orders = ordersService.getById(orderId);
        Business business = businessService.selectByUserId(orders.getCompanyid());
        return new CommonResult<>(200,"",business);
    }

}

